﻿<UserControl x:Class="JayDev.MediaScribe.View.SettingsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             xmlns:nmControls="clr-namespace:JayDev.MediaScribe.View.Controls"
             xmlns:nmCommon="clr-namespace:JayDev.MediaScribe.Common;assembly=MediaScribe.Common"
             xmlns:nmConverters="clr-namespace:JayDev.MediaScribe.Converters"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
             xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF4"         
             xmlns:effect="clr-namespace:GrayscaleEffect;assembly=GrayscaleEffect"
             
                xmlns:sysControls="clr-namespace:System.Windows.Controls;assembly=PresentationFramework"
                xmlns:sysWinInput="clr-namespace:System.Windows.Input;assembly=WindowsBase"
                xmlns:System="clr-namespace:System;assembly=mscorlib"
                xmlns:wpfx="clr-namespace:Xceed.Wpf.Toolkit;assembly=WPFToolkit.Extended"
             
             x:Name="SettingsViewName"
             d:DesignHeight="368" d:DesignWidth="1380">
    <UserControl.Resources>

        <nmConverters:BooleanToVisibilityConverter 
                x:Key="BooleanToVisibilityConverterFalseIsVisible" 
                True="Collapsed" 
                False="Visible" />
        <nmConverters:BooleanToVisibilityConverter 
                x:Key="BooleanToVisibilityConverterTrueIsVisible" 
                True="Visible" 
                False="Collapsed" />
        <nmConverters:InverseBooleanConverter 
                x:Key="InverseBooleanConverter"  />

        <nmConverters:EnumInfoLabelToStringConverter x:Key="EnumInfoLabelToStringConverter" />
        <nmConverters:EnumToTooltipTextBlockConverter x:Key="EnumToTooltipTextBlockConverter" />
        <nmConverters:EnumToStringConverter x:Key="EnumToStringConverter" />
        <nmConverters:ColourToBrushConverter x:Key="ColourToBrushConverter" />
        <nmConverters:ColourToNameStringConverter x:Key="ColourToNameStringConverter" />



        <ObjectDataProvider x:Key="dataFromHotkeyFunctionEnum"
       MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="nmCommon:HotkeyFunction"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <ObjectDataProvider x:Key="dataFromDirectionEnum"
       MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="nmCommon:Direction"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <ObjectDataProvider x:Key="dataFromModifierKeysEnum"
       MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="sysWinInput:ModifierKeys"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>

        <ObjectDataProvider x:Key="dataFromKeyEnum"
       MethodName="GetValues" ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="sysWinInput:Key"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>


        <DataTemplate x:Key="DefaultTemplate">
        </DataTemplate>

        <DataTemplate x:Key="SeekViewTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Path=Direction}" />
                <TextBlock Text=" " xml:space="preserve" />
                <TextBlock Text="{Binding Path=Seconds}" />
                <TextBlock Text=" seconds" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="SeekEditTemplate">
            <StackPanel Orientation="Horizontal">
                <ComboBox ItemsSource="{Binding Source={StaticResource dataFromDirectionEnum}}"
                          SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=SelectedItem.MyDirection}">
                </ComboBox>
                <TextBlock Text=" " xml:space="preserve" />
                <TextBox Text="{Binding Path=Seconds}" PreviewTextInput="TextBox_PreviewTextInput" />
                <TextBlock Text=" seconds" />
            </StackPanel>
        </DataTemplate>

        <DataTemplate x:Key="ColourViewTemplate">
            <StackPanel Orientation="Horizontal">
                <Rectangle Height="12" Width="12" Margin="4 0 0 0" Stroke="Black" StrokeThickness="0.5" Fill="{Binding Colour, Converter={StaticResource ColourToBrushConverter}}" />
                <TextBlock Text=" " xml:space="preserve" />
                <TextBlock Text="{Binding Path=Colour, Converter={StaticResource ColourToNameStringConverter}}" />

            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="ColourEditTemplate">
            <StackPanel Orientation="Horizontal">
                <wpfx:ColorPicker Name="ColorPicker1" Height="20" DisplayColorAndName="True" Width="150" SelectedColor="{Binding Path=Colour}" />
            </StackPanel>
        </DataTemplate>

        <Style TargetType="TextBox" x:Key="controlledTextBox">
            <EventSetter Event="PreviewTextInput" Handler="TextBox_PreviewTextInput" />
        </Style>

        <Style TargetType="DataGridCell" x:Key="dynamicCellBasedOnFunctionType" BasedOn="{StaticResource DataGridCellStyle1}">
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Function}" Value="{x:Static nmCommon:HotkeyFunction.Seek}" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditing}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=SeekDirection}" />
                                    <TextBlock Text=" " xml:space="preserve" />
                                    <TextBlock Text="{Binding Path=SeekSeconds}" />
                                    <TextBlock Text=" seconds" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Function}" Value="{x:Static nmCommon:HotkeyFunction.Seek}" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditing}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <ComboBox ItemsSource="{Binding Source={StaticResource dataFromDirectionEnum}}"
                          SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=SelectedItem.SeekDirection}">
                                    </ComboBox>
                                    <TextBlock Text=" " xml:space="preserve" />
                                    <TextBox Text="{Binding Path=SeekSeconds}" Style="{StaticResource controlledTextBox}" />
                                    <TextBlock Text=" seconds" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Function}" Value="{x:Static nmCommon:HotkeyFunction.NoteColour}" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditing}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Rectangle Height="12" Width="12" Margin="4 0 0 0" Stroke="Black" StrokeThickness="0.5" Fill="{Binding Colour, Converter={StaticResource ColourToBrushConverter}}" />
                                    <TextBlock Text=" " xml:space="preserve" />
                                    <TextBlock Text="{Binding Path=Colour, Converter={StaticResource ColourToNameStringConverter}}" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Function}" Value="{x:Static nmCommon:HotkeyFunction.NoteColour}" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditing}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <wpfx:ColorPicker Name="ColorPicker1" Height="20" DisplayColorAndName="True" Width="150" SelectedColor="{Binding Path=Colour}" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Function}" Value="{x:Static nmCommon:HotkeyFunction.NoteRating}" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditing}" Value="False" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBlock Text="{Binding Path=Rating}" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Function}" Value="{x:Static nmCommon:HotkeyFunction.NoteRating}" />
                        <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsEditing}" Value="True" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <TextBox Text="{Binding Path=Rating}" Style="{StaticResource controlledTextBox}" />
                                </StackPanel>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </MultiDataTrigger>
            </Style.Triggers>
            <Style.Setters>
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <ContentControl />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style.Setters>
        </Style>



        <Style TargetType="ComboBox" x:Key="selectionChangedHandledComboBox">
            <EventSetter Event="SelectionChanged" Handler="ComboBox_SelectionChanged" />
        </Style>


    </UserControl.Resources>
    <Grid Background="#FFEEF6FF">
        <DockPanel Name="windowContents">
            <!--<nmControls:NavigationControl2 DockPanel.Dock="Top"
                                                NavigateCommand="{Binding NavigateCommand}"
                                                />-->
            <Grid Name="mainGrid">


                <ScrollViewer DockPanel.Dock="Top" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto" Width="auto">
                    <DockPanel Grid.Column="1">
                        <StackPanel DockPanel.Dock="Top" MaxWidth="800">
                            <!--<GroupBox Header="Application Settings"  Foreground="#FF3F3F3F" Margin="10 10 10 0" Padding="10" BorderBrush="#FF75B2DE">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="250" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0" TextWrapping="Wrap">Load last course when the application starts:</TextBlock>
                                    <CheckBox Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Left"></CheckBox>
                                </Grid>
                            </GroupBox>-->
                            <GroupBox Header="Hotkeys" Foreground="#FF3F3F3F" BorderBrush="#FF75B2DE" Margin="10 10 10 10" Padding="5">
                                <DockPanel>
                                    <!-- make the border the same colour as the grid's border... we want to make it look like an extension of the grid. -->
                                    <StackPanel DockPanel.Dock="Bottom">
                                        <Border BorderThickness="1 0 1 1" BorderBrush="#A3AEB9" Background="#FFFF" Visibility="{Binding AreSettingsExisting, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverterFalseIsVisible}, UpdateSourceTrigger=PropertyChanged, FallbackValue=Visible}">
                                            <StackPanel MinHeight="30">
                                                <TextBlock Text="Please add some hotkeys!" Margin="0 5 0 0" HorizontalAlignment="Center"  VerticalAlignment="Center" />
                                            </StackPanel>
                                        </Border>
                                        <StackPanel Visibility="{Binding IsEditingHotkeys, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverterTrueIsVisible}, UpdateSourceTrigger=PropertyChanged, FallbackValue=Visible}" Orientation="Horizontal" VerticalAlignment="Top" HorizontalAlignment="Center" MinHeight="30">


                                            <Button Focusable="False" Style="{StaticResource minusButtonStyle}"  Command="{Binding DeleteHotkeysCommand}">
                                                <Path Data="M0,0 L70,0 99,0 99,41 70,41 0,41 z" Margin="0 0 0 0" Stretch="Uniform" Width="13.5" Height="5" Style="{DynamicResource minusStyle}"/>
                                            </Button>

                                            <Button Focusable="False" Margin="5 0 0 0" Style="{StaticResource plusButtonStyle}"  Click="addHotkeyButton_Click">
                                                <Path Data="M35,0 L70,0 70,30 100,30 100,70 70,70 70,100 30,100 30,70 0,70 0,30 35,30 z" Margin="0 0 0 0" Stretch="Fill" Width="13.5" Height="13" Style="{DynamicResource PlusStyle}"/>
                                            </Button>


                                        </StackPanel>

                                        <StackPanel Visibility="{Binding IsEditingHotkeys, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverterFalseIsVisible}, UpdateSourceTrigger=PropertyChanged, FallbackValue=Visible}" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0 15 0 10">
                                            <Button Padding="5 1" Command="{Binding HotkeysEditCommand}">Edit</Button>
                                            <Button Margin="10 0 0 0" Padding="5 1" Command="{Binding RestoreDefaultHotkeysCommand}">Restore defaults</Button>
                                        </StackPanel>
                                        <StackPanel Visibility="{Binding IsEditingHotkeys, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverterTrueIsVisible}, UpdateSourceTrigger=PropertyChanged, FallbackValue=Visible}" Orientation="Horizontal" HorizontalAlignment="Center" Margin="0 15 0 10">
                                            <Button Command="{Binding CancelCommand}" Padding="5 1">Cancel</Button>
                                            <Button Padding="5 1" Margin="10 0 0 0" Command="{Binding SaveHotkeysCommand}">Save</Button>
                                        </StackPanel>
                                    </StackPanel>
                                    <DockPanel>
                                        <TextBlock DockPanel.Dock="Top" Margin="0 0 0 5" TextWrapping="Wrap" Text="Double-click on the empty row at the bottom to add a new hotkey. Also, you can hover over the 'Function' entries to get a description of what it does, and when it's usable." />

                                        <nmControls:DragEnabledDataGrid  x:Name="hotkeyGrid" AutoGenerateColumns="False" HorizontalAlignment="Left" VerticalAlignment="Top"
                                                                          CanUserAddRows="{Binding IsEditingHotkeys, Mode=OneWay}" HeadersVisibility="Column"
                                                                          ItemsSource="{Binding Hotkeys}" 
                                                                          CanDrag="False"
                                                                          IsReadOnly="{Binding IsEditingHotkeys, Converter={StaticResource InverseBooleanConverter}}"
                                                                          ReadOnlySelectedItems="{Binding SelectedHotkeys, Mode=OneWayToSource}"
                                                                          Style="{StaticResource DataGridStyle1}">
                                            <DataGrid.Columns>

                                                <DataGridTemplateColumn Header="Function" Width="200" SortMemberPath="Function" CanUserSort="True">
                                                    <DataGridTemplateColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <TextBlock Text="{Binding Function, Converter={StaticResource EnumInfoLabelToStringConverter}}" ToolTip="{Binding Function, Converter={StaticResource EnumToTooltipTextBlockConverter}}" />
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellTemplate>
                                                    <DataGridTemplateColumn.CellEditingTemplate>
                                                        <DataTemplate>
                                                            <ComboBox Text="Select Hotkey Function..." x:Name="functionComboBox" ItemsSource="{Binding Source={StaticResource dataFromHotkeyFunctionEnum}}"
                                      SelectedItem="{Binding Function, Mode=TwoWay}" Style="{StaticResource selectionChangedHandledComboBox}">
                                                                <ComboBox.ItemTemplate>
                                                                    <DataTemplate>
                                                                        <!-- JDW NOTE: there's a problem with trying to have a tooltip on a comboboxitem, which interferes
                                                                    with clicking on the CONTENTS of the comboboxitem - it doesn't register the click, unless you click
                                                                    in the white space around the contents (i.e. next to the textblock, instead of on it). To make it
                                                                    clickable, we set IsHitTestVisible=False... but then it doesn't receive mouse events, and the
                                                                    tooltip won't show up. So I've made a custom ComboBoxItem type which sets the tooltip of the PARENT
                                                                    BORDER object, to the bound tooltip... this means the PARENT will show the tooltip correctly, and we
                                                                    can leave the comboboxitem with IsHitTestVisible=False! :D -->
                                                                        <nmControls:ComboBoxItemWithTooltip IsHitTestVisible="False" ToolTip="{Binding Path=., Converter={StaticResource EnumToTooltipTextBlockConverter}}">
                                                                            <nmControls:ComboBoxItemWithTooltip.Content>
                                                                                <TextBlock  Text="{Binding Path=., Converter={StaticResource EnumInfoLabelToStringConverter}, Mode=TwoWay}"  />
                                                                            </nmControls:ComboBoxItemWithTooltip.Content>
                                                                        </nmControls:ComboBoxItemWithTooltip>
                                                                    </DataTemplate>
                                                                </ComboBox.ItemTemplate>
                                                            </ComboBox>
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellEditingTemplate>
                                                </DataGridTemplateColumn>

                                                <DataGridTemplateColumn Header="Parameter" Width="165" CanUserSort="False" CellStyle="{StaticResource dynamicCellBasedOnFunctionType}" />

                                                <DataGridTemplateColumn Header="Modifier" Width="105" SortMemberPath="ModifierKey" CanUserSort="True">
                                                    <DataGridTemplateColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <TextBlock Text="{Binding ModifierKey, Converter={StaticResource EnumToStringConverter}}" />
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellTemplate>
                                                    <DataGridTemplateColumn.CellEditingTemplate>
                                                        <DataTemplate>
                                                            <ComboBox Text="Select modifier..." ItemsSource="{Binding Source={StaticResource dataFromModifierKeysEnum}}"
                                      SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=SelectedItem.ModifierKey, Mode=TwoWay}">
                                                            </ComboBox>
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellEditingTemplate>
                                                </DataGridTemplateColumn>

                                                <DataGridTemplateColumn Header="Key" Width="*" SortMemberPath="Key" CanUserSort="True">
                                                    <DataGridTemplateColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <TextBlock Text="{Binding Key, Converter={StaticResource EnumToStringConverter}}" />
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellTemplate>
                                                    <DataGridTemplateColumn.CellEditingTemplate>
                                                        <DataTemplate>
                                                            <StackPanel Orientation="Horizontal">
                                                                <!--FocusManager.FocusedElement="{Binding ElementName=hitTextbox}"-->
                                                                <ComboBox MinWidth="110" Text="Select key..." ItemsSource="{Binding Source={StaticResource dataFromKeyEnum}}"
                                          SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=SelectedItem.Key, Mode=TwoWay}">
                                                                </ComboBox>
                                                                <TextBox Margin="5 0 0 0" Text="Click here &amp; Press key" PreviewKeyDown="TextBox_PreviewKeyDown" Name="hitTextbox"></TextBox>
                                                            </StackPanel>
                                                        </DataTemplate>
                                                    </DataGridTemplateColumn.CellEditingTemplate>
                                                </DataGridTemplateColumn>
                                            </DataGrid.Columns>
                                            </nmControls:DragEnabledDataGrid>
                                    </DockPanel>
                                </DockPanel>
                            </GroupBox>
                        </StackPanel>
                        <!--Empty content control, which 'fills' the dockpanel. As the grid grows, this will shrink away to nothing... and as the grid continues to grow, it'll get scrollbars.-->
                        <ContentControl>
                        </ContentControl>
                    </DockPanel>
                </ScrollViewer>
            </Grid>
        </DockPanel>
    </Grid>
</UserControl>
